package com.shd.edanpei.ui.activity;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.shd.edanpei.R;
import com.shd.edanpei.base.BaseActivity;
import com.shd.edanpei.base.BaseIPresenter;
import com.shd.edanpei.base.data.RxBusCode;
import com.shd.edanpei.base.rxbus.RxBus;
import com.shd.edanpei.base.rxbus.Subscribe;
import com.shd.edanpei.ui.view.NewOrderView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by mc on 2018/9/19.
 * 新建客户订单
 */

public class NewCustomerOrderActivity extends BaseActivity {

    private final String[] tabTitle = {"住房订单", "车辆订单"};

    @BindView(R.id.headline)
    TextView headline;
    @BindView(R.id.tab_layout)
    TabLayout tabLayout;
    @BindView(R.id.view_pager)
    ViewPager viewPager;

    private List<View> viewList = new ArrayList<>();

    @Override
    protected int setLayout() {
        return R.layout.activity_newcustomerorder;
    }

    @Override
    protected BaseIPresenter bindPresenter() {
        return null;
    }

    @Override
    protected void initView(@Nullable Bundle savedInstanceState) {
        RxBus.get().register(this);
        RxBus.get().send(RxBusCode.HOME_CREATE_DIALOG_DISMISS);
        headline.setText("选择订单类型");
        viewPager.setAdapter(new ViewPagerAdapter());
        tabLayout.setupWithViewPager(viewPager);
        for (int i = 0; i < tabLayout.getTabCount(); i++) {
            tabLayout.getTabAt(i).setText(tabTitle[i]);
            viewList.add(new NewOrderView(this, i));
        }
    }

    @Override
    protected void initListener() {

    }

    @OnClick(R.id.iv_headline_back)
    void close() {
        finish();
    }

    private class ViewPagerAdapter extends PagerAdapter {

        @Override
        public int getCount() {
            return tabTitle.length;
        }

        @Override
        public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
            return view == object;
        }

        @NonNull
        @Override
        public Object instantiateItem(@NonNull ViewGroup container, int position) {
            container.addView(viewList.get(position));
            return viewList.get(position);
        }

        @Override
        public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
            container.removeView((View) object);
        }
    }

    @Subscribe(code = RxBusCode.ORDER_CREATE_SUCCESS)
    public void orderCreateSuccess() {
        finish();
    }

    @Override
    protected void onDestroy() {
        RxBus.get().unRegister(this);
        super.onDestroy();

    }
}
